//
//  AllClassifyTableViewController.m
//  DeliciousFoodBible
//
//  Created by lanou3g on 15/7/19.
//  Copyright (c) 2015年 孙连爽. All rights reserved.
//

#import "AllClassifyTableViewController.h"
#import "DetailClassifyTableViewController.h"
#import "AllClassifyTableViewCell.h"
#import "Model.h"

#define mainURL  @"http://apis.juhe.cn/cook/category?key=a8be7fa328b601bbb1e55ef1084f9bbb"

@interface AllClassifyTableViewController ()

@property (nonatomic,retain) Model *model;
@property (nonatomic,retain) NSArray *listArray;

@end

@implementation AllClassifyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backView.jpeg"]];
    self.title = @"分 类";
//  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"back.jpeg"] forBarMetrics:UIBarMetricsDefault];
    
    [self requestData];
    
    
}



//数据请求与解析
-(void) requestData{
    
    self.array = [NSArray array];
    NSURL *url = [[NSURL alloc] initWithString:mainURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *resopne = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&resopne error:&error];
    
    _model = [[Model alloc] init];
    _model.classDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    _model.array = [_model.classDic objectForKey:@"result"];
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailClassifyTableViewController *detailVC = [[DetailClassifyTableViewController alloc] init];
    detailVC.str = [_model.array[indexPath.row] objectForKey:@"name" ];
    detailVC.listArray = [_model.array[indexPath.row] objectForKey:@"list"];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   
    return _model.array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"reuseIdentifier";
    
   AllClassifyTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[AllClassifyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    //设置cell的文本
    cell.textLabel.text = [_model.array[indexPath.row] objectForKey:@"name"];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //设置cell背景图片
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back.jpeg"]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
